package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;
import java.util.Map;

import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.system.domain.Zhuanjiabiandongshenbao;
import com.ruoyi.system.service.IDeptService;
import com.ruoyi.system.tools.BooleanMessage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.ZhuanjiaDeptMapper;
import com.ruoyi.system.domain.ZhuanjiaDept;
import com.ruoyi.system.service.IZhuanjiaDeptService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 专家单位关系Service业务层处理
 * 
 * @author dy
 * @date 2024-05-29
 */
@Service
public class ZhuanjiaDeptServiceImpl implements IZhuanjiaDeptService 
{
    @Autowired
    private ZhuanjiaDeptMapper zhuanjiaDeptMapper;

    @Autowired
    private IDeptService deptService;

    /**
     * 查询专家单位关系
     * 
     * @param id 专家单位关系主键
     * @return 专家单位关系
     */
    @Override
    public ZhuanjiaDept selectZhuanjiaDeptById(String id)
    {
        return zhuanjiaDeptMapper.selectZhuanjiaDeptById(id);
    }

    /**
     * 查询专家单位关系列表
     * 
     * @param zhuanjiaDept 专家单位关系
     * @return 专家单位关系
     */
    @Override
    public List<ZhuanjiaDept> selectZhuanjiaDeptList(ZhuanjiaDept zhuanjiaDept)
    {
        return zhuanjiaDeptMapper.selectZhuanjiaDeptList(zhuanjiaDept);
    }

    /**
     * 新增专家单位关系
     * 
     * @param zhuanjiaDept 专家单位关系
     * @return 结果
     */
    @Override
    public int insertZhuanjiaDept(ZhuanjiaDept zhuanjiaDept)
    {
        return zhuanjiaDeptMapper.insertZhuanjiaDept(zhuanjiaDept);
    }

    /**
     * 修改专家单位关系
     * 
     * @param zhuanjiaDept 专家单位关系
     * @return 结果
     */
    @Override
    public int updateZhuanjiaDept(ZhuanjiaDept zhuanjiaDept)
    {
        return zhuanjiaDeptMapper.updateZhuanjiaDept(zhuanjiaDept);
    }

    /**
     * 批量删除专家单位关系
     * 
     * @param ids 需要删除的专家单位关系主键
     * @return 结果
     */
    @Override
    public int deleteZhuanjiaDeptByIds(String ids)
    {
        return zhuanjiaDeptMapper.deleteZhuanjiaDeptByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除专家单位关系信息
     * 
     * @param id 专家单位关系主键
     * @return 结果
     */
    @Override
    public int deleteZhuanjiaDeptById(String id)
    {
        return zhuanjiaDeptMapper.deleteZhuanjiaDeptById(id);
    }

    @Override
    public List<Map<String,Object>> selectZhuanjiaDeptListWithoutDie(ZhuanjiaDept zhuanjiaDept) {
        return zhuanjiaDeptMapper.selectZhuanjiaDeptListWithoutDie(zhuanjiaDept);
    }

    @Transactional
    @Override
    public BooleanMessage zhuanyi(String ppZhuanjiaIds, String ppDeptId) {

        boolean ok = true;

        String[] zhuanjiaids = ppZhuanjiaIds.split(",");
        for (int i = 0; i < zhuanjiaids.length; i++) {
            String mmZhuanjiaId = zhuanjiaids[i];

            //查询旧的关系
            ZhuanjiaDept zhuanjiaDept = new ZhuanjiaDept();
            zhuanjiaDept.setZhuanjiaid(mmZhuanjiaId);
            zhuanjiaDept.setStatus("1");

            List<ZhuanjiaDept> mmList = this.selectZhuanjiaDeptList(zhuanjiaDept);
            if (mmList.size() != 1) {
                return BooleanMessage.getErrorMessage("该单位专家查询出错");
            }
            ZhuanjiaDept tempDeptZhuanjia = mmList.get(0);

            if (ppDeptId.equals(tempDeptZhuanjia.getDeptid())) {
                return BooleanMessage.getErrorMessage("不可转移至原单位");
            }

            tempDeptZhuanjia.setStatus("2");
            tempDeptZhuanjia.setBiangengshijian(new Date());

            if (updateZhuanjiaDept(tempDeptZhuanjia) != 1) {
                ok = false;
                return BooleanMessage.getErrorMessage(tempDeptZhuanjia.getXingming() + "修改旧关系失败");
            }

            ZhuanjiaDept newZhuanjiaDept = new ZhuanjiaDept();
            newZhuanjiaDept.setId(UUID.randomUUID().toString());
            newZhuanjiaDept.setZhuanjiaid(mmZhuanjiaId);
            newZhuanjiaDept.setDeptid(ppDeptId);
            newZhuanjiaDept.setXingming(tempDeptZhuanjia.getXingming());
            newZhuanjiaDept.setDanweimingcheng(deptService.selectDeptByDeptid(ppDeptId).getDeptname());
            newZhuanjiaDept.setBiangengshijian(new Date());
            newZhuanjiaDept.setStatus("1");

            if (insertZhuanjiaDept(newZhuanjiaDept) != 1) {
                ok = false;
                return BooleanMessage.getErrorMessage(newZhuanjiaDept.getXingming() + "指派单位失败");
            }

            //添加变动申报,转隶

//            ZhuanjiaBiandongHistory mmZhuanjiaBiandongHistory = new ZhuanjiaBiandongHistory();
//            mmZhuanjiaBiandongHistory.setId(UUID.randomUUID().toString());
//            mmZhuanjiaBiandongHistory.setZhuanjiaid(mmZhuanjiaId);
//            mmZhuanjiaBiandongHistory.setOldzhuantai("10");
//            mmZhuanjiaBiandongHistory.setNewzhuangtai("11");
//            mmZhuanjiaBiandongHistory.setOlddanwei(deptService.selectDeptById(tempDeptZhuanjia.getDeptid()).getJigoumingcheng());
//            mmZhuanjiaBiandongHistory.setNewdanwei(deptService.selectDeptById(ppDeptId).getJigoumingcheng());
//            mmZhuanjiaBiandongHistory.setBiandongshijian(new Date());
//            mmZhuanjiaBiandongHistory.setOrdernum(zhuanjiaBiandongHistoryService.getMaxOrdernum(mmZhuanjiaId)+1);
//            mmZhuanjiaBiandongHistory.setIsvalid((long)1);
//
//            if (zhuanjiaBiandongHistoryService.insertZhuanjiaBiandongHistory(mmZhuanjiaBiandongHistory) != 1) {
//                ok = false;
//                return BooleanMessage.getErrorMessage(mmNewDeptZhuanjia.getXingming() + "变动历史添加失败");
//            }
        }

        return BooleanMessage.getSuccessMessage("转移成功");
    }

    @Override
    public ZhuanjiaDept findByZhuanjiaId(String ppZhuanjiaId) {
        ZhuanjiaDept mmZhuanjiaDept = new ZhuanjiaDept();
        mmZhuanjiaDept.setZhuanjiaid(ppZhuanjiaId);
        mmZhuanjiaDept.setStatus("1");
        return selectZhuanjiaDeptList(mmZhuanjiaDept).size() == 1 ? selectZhuanjiaDeptList(mmZhuanjiaDept).get(0) : null;
    }
}
